/*!
 * \file frreport_session_comm.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include <frdb_cursor.h>

#include "frdb_cursor.h"
#include "frreport_session_mng.h"
#include "frreport_session_comm.h"
#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


static Frhash_header *g_p_st_hash_data;
static Frhash_header *g_p_st_hash_node_free;
extern  pthread_spinlock_t g_sp_lock;

static Frexp_ref g_st_expRef={NULL, NULL};
static Frservice_ref g_st_serviceRef={NULL, NULL};
static Frreport_session_ref g_st_report_session_Ref={NULL, NULL, NULL};
static Report_module g_st_report_moduleRef = {NULL, NULL, NULL, NULL, NULL};
static ApmContext g_st_apm_context = {&g_st_expRef, &g_st_expRef, &g_st_serviceRef, &g_st_report_session_Ref, &g_st_report_moduleRef};

static void my_free(void **pp_data)
{
    frfree(*pp_data);
    return;
}

static void __case1(void)
{
    int i_ret = 0;
    void *p_data = (char *)frstrdup((char *)"test");

    i_ret = frreport_session_put(NULL, 1, (char *)"syncmodule", 10, p_data);
    frfree(p_data);

    p_data = frreport_session_get(NULL, 1, (char *)"syncmodule", 10);
    CU_ASSERT_PTR_NULL(p_data);

    return;

}

static void __case2(void)
{
    int i_ret = 0;
    void *p_data = (char *)frstrdup((char *)"test");

    i_ret = frreport_session_mng_init(0);


    frreport_session_mng_setRef(g_st_apm_context.p_st_report_session_ref);

    i_ret = frreport_session_set(&g_st_apm_context, 1, my_free);

    i_ret = frreport_session_put(&g_st_apm_context, 1, (char *)"syncmodule", 10, p_data);


    p_data = frreport_session_get(&g_st_apm_context, 1, (char *)"syncmodule", 10);

    CU_ASSERT_STRING_EQUAL((char *)p_data, (char *)"test");
    frreport_session_mng_clean();

    frfree(p_data);

    frreport_session_mng_destory();
    return;

}


static void __case3(void)
{
    int i_ret = 0;
    void *p_data = (char *)frstrdup((char *)"test");

    i_ret = frreport_session_put(NULL, 1, (char *)"syncmodule", 10, p_data);
    frfree(p_data);

    p_data = frreport_session_get(NULL, 1, (char *)"syncmodule", 10);
    CU_ASSERT_PTR_NULL(p_data);

    return;

}

static void __case4(void)
{
    int i_ret = 0;
    void *p_data = (char *)frstrdup((char *)"test");

    i_ret = frreport_session_mng_init(0);


    frreport_session_mng_setRef(g_st_apm_context.p_st_report_session_ref);

    i_ret = frreport_session_set(&g_st_apm_context, 1, my_free);

    i_ret = frreport_session_put(&g_st_apm_context, 1, (char *)"syncmodule", 10, p_data);


    p_data = frreport_session_get(&g_st_apm_context, 1, (char *)"syncmodule", 10);

    CU_ASSERT_STRING_EQUAL((char *)p_data, (char *)"test");
    frreport_session_mng_clean();

    frfree(p_data);

    frreport_session_mng_destory();
    return;

}



static void __case5(void)
{
    int i_ret = 0;

    i_ret = frreport_session_set(NULL, 1, my_free);
    CU_ASSERT_EQUAL(i_ret, FR_FAIL);
    return;

}


static void __case6(void)
{
    int i_ret = 0;
    Frreport_session_data *p_session_data = NULL;
    Frlist_node *p_st_node = NULL;

    i_ret = frreport_session_mng_init(0);

    p_session_data = (Frreport_session_data *)frmalloc(sizeof(Frreport_session_data));
    memset(p_session_data, 0x00, sizeof(Frreport_session_data));

    p_session_data->i_l7_proto = 1;
    p_session_data->p_data = (char *)frstrdup((char *)"test");

    p_st_node = frlist_node_create(p_session_data);

    frhash2_addLong(g_p_st_hash_data, p_session_data->i_l7_proto, p_st_node);

//    p_st_node = frlist_node_create(my_free);
    frreport_session_mng_setRef(g_st_apm_context.p_st_report_session_ref);

    i_ret = frreport_session_set(&g_st_apm_context, 1, my_free);
    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    frhash2_addLong(g_p_st_hash_node_free, 1, p_st_node);

    frreport_session_mng_clean();

    frfree(p_st_node);
    frfree(p_session_data->p_data );
    frfree(p_session_data);
    frreport_session_mng_destory();
    return;

}

int add_suite_frreport_session_comm(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frreport_session_comm frreport_session_comm()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);
    CU_add_test(pSuite, "case6", __case6);

    return FR_SUCC;
}
